package com.dly.blog.service.impl;

import com.dly.blog.domain.Column;
import com.dly.blog.mapper.ColumnMapper;
import com.dly.blog.service.ColumnService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.dly.blog.domain.vo.column.ColumnVO;
import com.dly.blog.domain.ro.column.ColumnPageRO;
import com.dly.blog.domain.ro.column.ColumnAddRO;
import com.dly.blog.domain.ro.column.ColumnEditRO;
import com.dly.blog.domain.vo.PageVO;
import org.springframework.beans.BeanUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.transaction.annotation.Transactional;
import lombok.AllArgsConstructor;
import cn.hutool.core.collection.ListUtil;

/**
 * <p>
 * 专栏 服务实现类
 * </p>
 *
 * @author DLY
 * @since 2024-01-24 09:53:18
 */
@Service
@AllArgsConstructor
public class ColumnServiceImpl extends ServiceImpl<ColumnMapper, Column> implements ColumnService {

    @Override
    public PageVO<ColumnVO> getColumnPage(ColumnPageRO columnPageRO) {
        Page<Column> page = baseMapper.selectPage(columnPageRO.buildIPage(), new LambdaQueryWrapper<>());
        return PageVO.buildMyBatis(page, column -> {
            ColumnVO columnVO = new ColumnVO();
            BeanUtils.copyProperties(column, columnVO);
            return columnVO;
        });
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addColumn(ColumnAddRO columnAddRO) {
        Column column = new Column();
        BeanUtils.copyProperties(columnAddRO, column);
        baseMapper.insert(column);
    }

    @Override
    public ColumnVO getColumn(String id) {
        Column column = baseMapper.selectById(id);
        ColumnVO columnVO = new ColumnVO();
        BeanUtils.copyProperties(column, columnVO);
        return columnVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeColumn(String[] ids) {
        baseMapper.deleteBatchIds(ListUtil.toList(ids));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editColumn(ColumnEditRO columnEditRO) {
        Column column = new Column();
        BeanUtils.copyProperties(columnEditRO, column);
        baseMapper.updateById(column);
    }

}
